<?php
/**
 * 工具函数包，系统加载的时候自动加载
 * 
 * @author Lee<a605333742@gmail.com>
 * @time    2016-07-24
 */

/*
 * 根据节点id获取节点名
 * 
 * @param   $node_id    节点id
 * 
 * @return  str $nodeName
 */
function getNodeName($node_id=''){
    if($node_id=="" ||$node_id==0){
        return "顶级节点";
    }
    //if($nodeName=S("nodeName")){
    //    return $nodeName[$node_id];
    //}
    $node       =D("Node");
    $nodeList   =$node->select(array('field' => 'id,node_name'));
    $data       =array();
    foreach($nodeList as $vo){
        $data[$vo['id']]=$vo['node_name'];
    }
    //S("nodeName",$data,3600*24);
    $nodeName   =$data[$node_id];    
    return $nodeName;
}


/*
 * 异位或加密和解密
 * 
 * @param   $value 需要加密的字符
 * @param   $type  操作类型，不存在为解密，默认为解密
 * 
 * @reurn   str $value处理过的字符串
 */
function encode($value,$type=''){
    //$type不存在为解密，默认为解密
    $key	=md5(C('AUTO_LOGIN_KEY'));
    if($type){
        //加密
        return str_replace("=",'',base64_encode($value ^ $key));
    }
    //解密
    $value	=base64_decode($value);
    return  $value ^ $key;
}

/*
 * 根据id获取用户名
 * 
 * @param   $userId 用户id
 * 
 * @return str  $usename
 */
function getUserName($u_id=''){
    if($u_id==''){
        return "";
    }
    //if($userName=S("username",$data)){
    //    return $userName[$u_id];
    //}
    $user       =D("User");
    $userList   =$user->select(array("field"=>"id,nickname"));
    $data       =array();
    foreach($userList as $vo){
        $data[$vo['id']]=$vo['nickname'];
    }
    S("username",$data,3600*12);
    $userName   =$data[$u_id];
    return $userName;
}

/*
 * 根据id获取地区名
 * 
 * @param   $regionId   地区id
 *
 * @return str  regionName
 */
function getRegion($regionId=''){
    if($regionId==''){
        return "";
    }
    //if($regionName=S("regionName")){
    //    return $regionName[$regionId];
    //}
    $region     =D("Region");
    $regionList =$region->select(array("file"=>"id,name"));
    $data       =array();
    foreach ($regionList as $vo){
        $data[$vo['id']]=$vo['name'];
    }
    S("regionName",$data,3600*12);
    $regionName =$data[$regionId];
    return $regionName;
}

/*
 * 根据用户id获取用户电话
 * 
 * @param   $userId 用户id
 * 
 * @return  str tel
 */
function getTel($userId=''){
    if($userId==''){
        return "";
    }
    //if($tel=S("tel")){
    //    return $tel[$userId];
    //}
    $user       =D("User");
    $userList   =$user->select(array("field"=>"id,tel"));
    $data       =array();
    foreach($userList as $vo){
        $data[$vo['id']]=$vo['tel'];
    }
    S("tel",$data,3600*12);
    $tel    =$data[$userId];
    return $tel;
}

/*
 * 根据用户id获取用户公司名称
 * 
 * @return str companyName
 */
function getCompany($user_id=''){
    if($user_id==''){
        return "";
    }
    $user       =D("User");
    $userList   =$user->select(array("field"=>"id,companyName"));
    $data       =array();
    foreach($userList as $vo){
        $data[$vo['id']]=$vo['companyName'];
    }
    $companName =$data[$user_id];
    return $companName;
}
    
/*
 * 根据用户id获取用户地址
 * 
 * @param   $userId 用户id
 * 
 * @return str address

function getAdd($userId=''){
    if($userId==''){
        return "";
    }
    if($address=S("address")){
        return $address[$userId];
    }
    $user       =D("User");
    $userList   =$user->select(array("field"=>"id,address"));
    $data       =array();
    foreach($userList as $vo){
        $data[$vo['id']]=$vo['address'];
    }
    S("address",$data,3600*12);
    $address    =$data[$userId];
    return $address;
}
 * 
 */

/*
 * 状态值
 * 
 * @param   $status 状态值
 * @param   $imageShow 图片
 * 
 * @return str  state
 */
function changeState($state,$id,$type){
    switch ($state) {
        case 2 :
            $info = '<a href="'.U(CONTROLLER_NAME.'/audit',array('id'=>$id,'type'=>$type)) . '" target="ajaxTodo">待审</a>';
            break;
        case 1 :
            $info = '<a href="'.U(CONTROLLER_NAME.'/pass',array('id'=>$id,'type'=>$type)) . '" target="ajaxTodo">批准</a>';
            break;
    }
    return $info;
}

/*
 * 根据状态显示相应的图片
 * 
 * @return  #
 */
function getStatus($status, $imageShow = true){
    switch ($status) {
        case 0 :
            $showText = '禁用';
            $showImg = '<IMG SRC="/tuyouApp/Application/Public/Images/locked.gif" WIDTH="20" HEIGHT="20" BORDER="0" ALT="禁用">';
            break;
        case 1 :
            $showText = '待审';
            $showImg = '<IMG SRC="/tuyouApp/Application/Public/Images/prected.gif" WIDTH="20" HEIGHT="20" BORDER="0" ALT="待审">';
            break;
        case 2 : 
            $showText = '正常';
            $showImg = '<IMG SRC="/tuyouApp/Application/Public/Images/ok.gif" WIDTH="20" HEIGHT="20" BORDER="0" ALT="正常">';
        break;
        default :
            $showText = '删除';
            $showImg = '<IMG SRC="/tuyouApp/Application/Public/Images/del.gif" WIDTH="20" HEIGHT="20" BORDER="0" ALT="删除">';
            break;    
    }
    return ($imageShow === true) ? $showImg : $showText;
}

/*
 * 根据type获取帖子类型
 * 
 * @param   $type  帖子类型id
 * 
 * @return str typeName
 */
function getDocType($type=''){
    if($type==""){
        return "";
    }
    $doctype    =D("Doctorquesttype");
    $typeList   =$doctype->select(array("field"=>"id,name"));
    $data       =array();
    foreach($typeList as $vo){
        $data[$vo['id']]=$vo['name'];
    }
    //S("tel",$data,3600*12);
    $typeName   =$data[$type];
    return $typeName;
}

/*
 * 根据id获取关联的帖子
 * 
 * @param $doc_id  涂医生id
 * 
 * @return  str $docName
 */
function getDoc($doc_id=''){
    if($doc_id==''){
        return "";
    }    
    $doc    =D("Doctor");
    $docList=$doc->select(array("field"=>"id,title"));
    $data   =array();
    foreach($docList as $vo){
        $data[$vo['id']]=$vo['title'];
    }
    //S("tel",$data,3600*12);
    $docName    =$data[$doc_id];
    return $docName;
}

/*
 * 根据岗位id获取岗位
 * 
 * @return str $jobName
 */
function getJob($job_id=''){
    if($job_id==''){
        return '';
    }
    $job    =D("RecruitsJobType");
    $jobList=$job->select(array("field"=>"jobId,jobName"));
    $data   =array();
    foreach($jobList as $vo){
        $data[$vo['jobId']] =$vo['jobName'];
    }
    $jobName=$data[$job_id];
    return $jobName;
}

/*
 * 根据薪水id获取薪水
 * 
 * @return str  $salaryName
 */
function getSalary($salary_id=''){
    if($salary_id==''){
        return '';
    }
    $salary     =D("RecruitsSalaryType");
    $salaryList =$salary->select(array("field"=>"salaryId,salaryName"));
    $data       =array();
    foreach($salaryList as $vo){
        $data[$vo['salaryId']]  =$vo['salaryName'];
    }
    $salaryName =$data[$salary_id];
    return $salaryName;
}







